//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ruoyi.web.controller.exam;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.live.exam.service.LiveExamService;
import com.ruoyi.live.base.domain.LiveExam;
import com.ruoyi.live.base.domain.LiveExamClass;
import com.ruoyi.live.base.domain.LiveExamUser;
import com.ruoyi.live.base.mapper.LiveExamClassMapper;
import com.ruoyi.live.tool.utils.Message;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 竞赛列表管理
 */
@RestController("oldLiveExamController")
@RequestMapping({"/race/comptition/v1"})
public class LiveExamController extends BaseController {
    @Autowired
    LiveExamService service;
    @Autowired
    LiveExamClassMapper classDao;

    @GetMapping("/list")
    public TableDataInfo query(LiveExam entity) {
        this.startPage();
        List<LiveExam> list = this.service.query(entity);
        return this.getDataTable(list);
    }

    /**
     * 根据竞赛实体编号获取详细信息
     */
    @GetMapping(value = "/{examId}")
    public AjaxResult getInfo(@PathVariable("examId") Long examId)
    {
        return AjaxResult.success(service.getLiveExam(examId));
    }

    @PreAuthorize("@ss.hasAnyRoles('teacher,admin')")
    @Log(title = "竞赛列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(LiveExam entity, @Nullable @RequestParam("file") MultipartFile file, HttpServletRequest request) {
        try {
            this.service.addLiveExam(entity, file, request);
            return AjaxResult.success("新增成功");
        } catch (Exception exception) {
            exception.printStackTrace();
            return AjaxResult.error(exception.getMessage());
        }
    }

    @PreAuthorize("@ss.hasAnyRoles('teacher,admin')")
    @Log(title = "竞赛列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(LiveExam entity, @Nullable @RequestParam("file") MultipartFile file, HttpServletRequest request) {
        try {
            this.service.updateLiveExam(entity, file, request);
            return AjaxResult.success("修改成功");
        } catch (Exception exception) {
            exception.printStackTrace();
            return AjaxResult.error(exception.getMessage());
        }
    }

    @PreAuthorize("@ss.hasAnyRoles('teacher,admin')")
    @Log(title = "竞赛列表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{examIds}")
    public AjaxResult delete(@PathVariable Long[] examIds) {
        try {
            this.service.deleteLiveExam(examIds);
            return AjaxResult.success("删除成功");
        } catch (Exception exception) {
            exception.printStackTrace();
            return AjaxResult.error(exception.getMessage());
        }
    }

    @RequestMapping({"/LiveExamController/area"})
    public ModelAndView area(@Nullable Long id) {
        if (id == null) {
            LiveExam exam = this.service.getExam();
            if (exam != null) {
                id = exam.getId();
                Map<String, Integer> datas = this.service.getDatas(id);
                return (new ModelAndView("exam/live_exam_area")).addObject("exam", exam).addObject("datas", datas);
            }
        }

        return new ModelAndView("exam/live_exam_area");
    }

    /**
     * 查看绑定班级
     * @param examId
     * @return
     */
    @RequestMapping({"/LiveExamController/cls"})
    public ModelAndView cls(Long examId) {
        List<LiveExamClass> list = this.classDao.selectList(new QueryWrapper((new LiveExamClass()).setExamId(examId)));
        List<Long> ids = new ArrayList();
        Iterator var4 = list.iterator();

        while(var4.hasNext()) {
            LiveExamClass lec = (LiveExamClass)var4.next();
            ids.add(lec.getClassId());
        }

        return (new ModelAndView("exam/live_exam_class_list")).addObject("ids", StringUtils.join(ids, ","));
    }

    /**
     * 绑定班级
     * @param id
     * @param examId
     * @return
     */
    @ResponseBody
    @RequestMapping({"/LiveExamController/bind"})
    @PreAuthorize("@ss.hasAnyRoles('teacher,admin')")
    public Message bind(String id, Long examId) {
        try {
            this.service.doBind(id.split(","), examId);
            return new Message(true, "绑定成功");
        } catch (Exception var4) {
            var4.printStackTrace();
            return new Message(false, var4.getMessage());
        }
    }



    @ResponseBody
    @RequestMapping({"/LiveExamController/queryUserExamScore"})
    public TableDataInfo queryUserExamScore(LiveExamUser entity) throws Exception {
        this.startPage();
        List<LiveExamUser> list = this.service.queryUserExamScore(entity);
        return this.getDataTable(list);
    }

    @ResponseBody
    @RequestMapping({"/LiveExamController/closeExam"})
    @PreAuthorize("@ss.hasAnyRoles('teacher,admin')")
    public Message closeExam(Long id) {
        try {
            this.service.doCloseExam(id);
            return new Message(true, "关闭成功");
        } catch (Exception var3) {
            var3.printStackTrace();
            return new Message(false, "关闭失败");
        }
    }

    @ResponseBody
    @RequestMapping({"/LiveExamController/startExam"})
    @PreAuthorize("@ss.hasAnyRoles('teacher,admin')")
    public Message startExam(Long id) {
        try {
            this.service.startExam(id);
            return new Message(true, "开启成功");
        } catch (Exception var3) {
            var3.printStackTrace();
            return new Message(false, "开启失败");
        }
    }

    @ResponseBody
    @RequestMapping({"/LiveExamController/changeExamState"})
    public Message changeExamState(Long id, Integer state) {
        try {
            this.service.changeExamState(id, state);
            return new Message(true, "开启成功");
        } catch (Exception var4) {
            var4.printStackTrace();
            return new Message(false, "开启失败");
        }
    }

    @ResponseBody
    @RequestMapping({"/LiveExamController/delay"})
    @PreAuthorize("@ss.hasAnyRoles('teacher,admin')")
    public Message delay(String id, Integer count) {
        try {
            this.service.doDelay(id.split(","), count);
            return new Message(true, "延时成功");
        } catch (Exception var4) {
            var4.printStackTrace();
            return new Message(false, var4.getMessage());
        }
    }

    @ResponseBody
    @RequestMapping({"/LiveExamController/changeState"})
    @PreAuthorize("@ss.hasAnyRoles('teacher,admin')")
    public Message changeState(String id) {
        try {
            this.service.doChangeState(id.split(","));
            return new Message(true, "修改成功");
        } catch (Exception var3) {
            var3.printStackTrace();
            return new Message(false, var3.getMessage());
        }
    }



    @RequestMapping({"/LiveExamController/getScoreDetail"})
    @PreAuthorize("@ss.hasAnyRoles('teacher,admin')")
    public ModelAndView view(Long id) {
        return (new ModelAndView("/exam/live_exam_score_detail")).addObject("list", this.service.getScoreDetail(id));
    }

    @ResponseBody
    @RequestMapping({"/LiveExamController/reCountObjScore"})
    public Message reCountObjScore(Long id) {
        try {
            this.service.doCountObjScore(id);
            return new Message(true, "");
        } catch (Exception var3) {
            var3.printStackTrace();
            return new Message(false, "");
        }
    }

    @ResponseBody
    @RequestMapping({"/LiveExamController/reCountAllUser"})
    @PreAuthorize("@ss.hasAnyRoles('teacher,admin')")
    public Message reCountAllUser(Long id) {
        try {
            this.service.reCountAllUser(id);
            return new Message(true, "");
        } catch (Exception var3) {
            var3.printStackTrace();
            return new Message(false, "");
        }
    }

    @ResponseBody
    @RequestMapping({"/LiveExamController/reCountExpert"})
    @PreAuthorize("@ss.hasAnyRoles('teacher,admin')")
    public Message reCountExpert(Long id) {
        try {
            this.service.reCountExpert(id);
            return new Message(true, "");
        } catch (Exception var3) {
            var3.printStackTrace();
            return new Message(false, "");
        }
    }

    @ResponseBody
    @RequestMapping({"/LiveExamController/reCountScore"})
    @PreAuthorize("@ss.hasAnyRoles('teacher,admin')")
    public Message reCountScore(Long id) {
        try {
            this.service.reCountScore(id);
            return new Message(true, "");
        } catch (Exception var3) {
            var3.printStackTrace();
            return new Message(false, "");
        }
    }

    @RequestMapping({"/LiveExamController/excelScore"})
    @PreAuthorize("@ss.hasAnyRoles('teacher,admin')")
    public void excelScore(Long id, HttpServletResponse response) throws IOException {
        this.service.excelScore(id, response);
    }

    @RequestMapping({"/LiveExamController/getExpertScore"})
    @PreAuthorize("@ss.hasAnyRoles('teacher,admin')")
    public ModelAndView getExpertScore(Long id) {
        return this.service.getExpertScore(id);
    }

    @ResponseBody
    @RequestMapping({"/LiveExamController/editServerIp"})
    public Message editServerIp(String ip) {
        try {
            this.service.editServerIp(ip);
            return new Message(true, "");
        } catch (Exception var3) {
            var3.printStackTrace();
            return new Message(false, "");
        }
    }
}
